package com.xbn.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author XBN
 * @date 2020/10/13 17:05
 * @email 78742541@qq.com
 */
public class FileChannelRead {
    public static void main(String[] args) throws IOException {
        //创建文件的输入流
        File file = new File("L:\\tempFile\\channel.txt");
        FileInputStream inputStream = new FileInputStream(file);
        //通过输入流对象获取FileChannel
        FileChannel fileChannel =inputStream.getChannel();
        //创建缓冲区
        ByteBuffer byteBuffer=ByteBuffer.allocate((int)file.length());
        //将通道的数据读入到buffer中
        fileChannel.read(byteBuffer);
        // 将byteBuffer的字节数据转换成String
        System.out.println(new String(byteBuffer.array()));
        fileChannel.close();
    }
}
